function submitContest(){
    
    var description = $('#description').val();
    var title = $('#title').val();
    var startDate = $('#startDate').val();
    var stopDate = $('#stopDate').val();
    if(!check_title(title)){
        if(title == "")
            alert('Devi inserire un titolo');
        else
            alert('Ci sono caratteri non ammessi nel titolo');
        return;
    }
    if(!check_description(description)){
        if(description == "")
            alert('Devi inserire una descrizione');
        else
            alert('Ci sono dei caratteri non ammessi nella descrizone');
        return;
    }
    if(!check_date_mdY(startDate)){
        if(startDate == "")
            alert('Devi inserire una data d\'inizio');
        else
            alert('Controlla il formato della data. Ti consigliamo di usare il datepicker');
        return;
    }
    if(!check_date_mdY(stopDate)){
        if(stopDate == ""){}
        else
            alert('Controlla il formato della data. Ti consigliamo di usare il datepicker');
        return;
    }
    $("#contestForm").submit();
        
}

function getContestInfo(id){
    var querystring = "action=getContestInfo&id="+id;
    $("#contestInfo").html("");
    showLoading();
    disableClick();
    $.ajax({
        method: "get",
        url: location.href.substring(0,location.href.lastIndexOf('/')+1)+"admin-ajax.php",
        data: querystring, 
        success: function(response){
            hideLoading();
            enableClick();
            $("#contestInfo").html(response);
        }
        })
    }
    
function contestSubscription(containerid,contestid){
    
    var querystring = "action=contestSubscription&id="+contestid;
    showLoading();
    disableClick();
    $.ajax({
        method: "get",
        url: location.href.substring(0,location.href.lastIndexOf('/')+1)+"wp-admin/admin-ajax.php",
        data: querystring, 
        success: function(response){
            if(response.match(/success/)){
                $("#"+containerid).fadeOut("slow",function(){
                    $("#"+containerid).html("<div class='contInscSuccess'><strong>Richiesta di iscrizione avvenuta con successo. Sarai contattato per email per la conferma dell'iscrizione</strong></div>");
                    hideLoading();
                    $("#"+containerid).fadeIn("slow",function(){
                        enableClick();
                    });
                    setTimeout("reloadContestsWidget('contestContainer')",3500);
                });
                }
        }
        })
    }
    
function allow_disallowUserInContest(userid,containerid,action){
    var contestid   = $("#contestid").attr("value");
    var querystring = "action=allow_disallowUserInContest&userid="+userid+"&allowdisallow="+action+"&contestid="+contestid;
    showLoading();
    disableClick();
    $.ajax({
        method: "get",
        url: location.href.substring(0,location.href.lastIndexOf('/')+1)+"admin-ajax.php",
        data: querystring, 
        success: function(response){
            hideLoading();
            if(response.match(/success/)){
                $("#"+containerid).fadeOut("slow",function(){
                    if(action == "allow")
                        $("#"+containerid).html("<div class='contInscSuccess'><strong>Utente ammesso al concorso</strong></div>");
                    else
                        $("#"+containerid).html("<div class='contInscSuccess'><strong>Utente eliminato dal concorso</strong></div>");
                    $("#"+containerid).fadeIn("slow",function(){
                        enableClick();
                    });
                });
                }
        }
        })
}

function allowVideoInContest(infoContainer,allow_disallow){
    var vidid = $("#"+infoContainer).attr("vidid");
    var username = $("#"+infoContainer).attr("username");
    var contestid = $("#"+infoContainer).attr("contestid")
    var querystring = 'action=allow_disallowVideoContest&videoid='+vidid+"&allowdisallow="+allow_disallow+"&contestid="+contestid+"&username="+username;
    showLoading();
    disableClick();
    $.ajax({
        method: "get",
        url: location.href.substring(0,location.href.lastIndexOf('/')+1)+"admin-ajax.php",
        data: querystring, 
        success: function(response){
            hideLoading();
            enableClick();
            if(response.match(/success/)){
                if(allow_disallow=="allow")
                    removeVideoFromNotAllowed(infoContainer,"Video Ammesso");
                else
                    removeVideoFromNotAllowed(infoContainer,"Video Eliminato");
                 closeVideoPlay();
        }
        }
    });
}



function getUploadPage(containerid,contestid){
    var querystring = "action=getUploadPage&contestid="+contestid;
    showLoading();
    disableClick();
    $.ajax({
        method: "get",
        url: location.href.substring(0,location.href.lastIndexOf('/')+1)+"admin-ajax.php",
        data: querystring, 
        success: function(response){
            hideLoading();
            $("#"+containerid).html(response);
            enableClick();
            hideLoading();
                
        }
        })
}

function removeVideoFromNotAllowed(infoContainer,allowed_disallowed_message){
    allowed_disallowed_message = allowed_disallowed_message || "";
    $("#"+infoContainer).parent().fadeOut(100,function(){
        $(this).replaceWith("<td><strong>"+allowed_disallowed_message+"</strong></td>");
        $(this).fadeIn(100);
        });
}


function nextPagination_contest(actual_item,item_nr){
    $("#vidwiz_next_pagenav-"+actual_item).hide();
    $("#vidwiz_prev_pagenav-"+actual_item).hide();
    actual_item = parseInt(actual_item);
    item_nr = parseInt(item_nr)-1;
    $("#contestElementCont-"+(actual_item)).fadeOut("slow",function(){
        $("#contestElementCont-"+(actual_item+1)).fadeIn("slow");
        $("#vidwiz_prev_pagenav-"+(actual_item+1)).show();
        if((actual_item+1) >= item_nr )
            $("#vidwiz_next_pagenav-"+(actual_item+1)).hide();
    });
}

function prevPagination_contest(actual_item,item_nr){
    $("#vidwiz_next_pagenav-"+actual_item).hide();
    $("#vidwiz_prev_pagenav-"+actual_item).hide();
    actual_item = parseInt(actual_item);
    item_nr = parseInt(item_nr)-1;
    $("#contestElementCont-"+(actual_item)).fadeOut("slow",function(){
        $("#contestElementCont-"+(actual_item-1)).fadeIn("slow");
        $("#vidwiz_next_pagenav-"+(actual_item-1)).show();
        if((actual_item-1) <= 0 )
            $("#vidwiz_prev_pagenav-"+(actual_item-1)).hide();
    });
}

function reloadContestsWidget(containerid){
    var querystring = "action=reloadContestsWidget";
    $.ajax({
        method: "get",
        url: location.href.substring(0,location.href.lastIndexOf('/')+1)+"wp-admin/admin-ajax.php",
        data: querystring, 
        success: function(response){
                $("#"+containerid).fadeOut("slow",function(){
                    $("#"+containerid).html(response);
                    hideLoading();
                    $("#"+containerid).fadeIn("slow");
                });
                }
        })
    }
    
var statuses = new Array();
    statuses[0] = "subscription";
    statuses[1] = "validation";
    statuses[2] = "voting";
    
function contestsHide(status){
    for(var i=0;i<statuses.length;i++){
        if(statuses[i] != status && status != "none"){
            $(".contestStatus-"+statuses[i]).fadeOut(300);
            }
        else
            $(".contestStatus-"+statuses[i]).fadeIn(300);
        }
    
}

function showVideoForVoting(userid,contestid){
    var querystring = "action=showVideoForVoting&contestid="+contestid+"&userid="+userid;
    showLoading();
    disableClick();
    $.ajax({
        method: "get",
        url: location.href.substring(0,location.href.lastIndexOf('/')+1)+"admin-ajax.php",
        data: querystring, 
        success: function(response){
            hideLoading();
            var video = getArray(response);
            playVideoContestVoting(video);
            enableClick();
                
        }
        })
}


function playVideoContestVoting(videoInfos){
    var newPath;
    var blackdiv = "<div class='blackdiv'></div>"
    $("#eventi").append(blackdiv);
    $(".blackdiv").css("width",$(document).width()+"px");
    $(".blackdiv").css("height",$(document).height()+"px");
    $(".blackdiv").fadeIn(700,function(){
        var newPath = videoInfos['file_position'];
        var plwidth = 500;
        var plheight = 350;
        var padding = 50;
        var close = "<div onclick='closeVideoPlay();' class='closeButton'></div>";
        var palette = "<div  id='palette'>"+close+"</div>";
        var player = "<div id='player'><div id='player_in'></div></div>";
        var margin_lateral = parseInt(($(window).width()-plwidth)/2);
        var margin_top_bottom = parseInt(($(window).height()-plheight)/2);
        $("#eventi").append(player);
        $("#player").css("display","none");
        $("#player").css("z-index","1000");
        $("#player").css("position","absolute");
        $("#player").css("background-color","white");
        $("#player").css("padding",padding+"px");
        $("#player").css("top","0");
        $("#player").css("left","0");
        $("#player").css("margin-left",margin_lateral+"px");
        $("#player").css("margin-right",margin_lateral+"px");
        $("#player").css("margin-bottom",margin_top_bottom+"px");
        $("#player").css("margin-top",margin_top_bottom+"px");
        $("#player").css("width",plwidth+"px");
        $("#player").css("height",plheight+"px");
        $("#player").show("slow",function(){
            jwplayer("player_in").setup({
              flashplayer: '../wp-content/plugins/video-wizard/libs/jwplayer/player.swf',
              file: newPath,
              autoplay: true,
              controlbar: "bottom",
              width: plwidth,
              height: plheight,
              provider: "http",
              "http.startparam":"start"
             });
           
           $("#eventi").append(palette);
           var palette_margin_lat = $(window).width()+(2*padding)-margin_lateral+10;
           var palette_margin_top = $(window).height()-($(window).height()-(2*margin_top_bottom)+(3*padding));
           $("#palette").css('margin-left',palette_margin_lat+"px");
           $("#palette").css('margin-top',palette_margin_top+"px");
           $("#palette").show('slow');
           
        });
    });
}
    
function deleteUserFromCharts(userid,contestid){
    var querystring = "action=deleteUserFromCharts&contestid="+contestid+"&userid="+userid;
    showLoading();
    disableClick();
    $.ajax({
        method: "get",
        url: location.href.substring(0,location.href.lastIndexOf('/')+1)+"admin-ajax.php",
        data: querystring, 
        success: function(response){
            
            var position = getValue(response);
            
            $("#user-buttons-"+userid).fadeOut(390,function(){
                if(position != 1){
                var inputs = $(".inputChart").get();
                for(var i=0;i<inputs.length;i++)
                    $(inputs[i]).attr("value",(position-1)+"° classificato");
                }
                $("#user-"+userid).removeClass("notInCharts");
                $("#user-buttons-"+userid).html("<div style='font-size:20px;'><strong>"+position+"°</strong></div>");
                $("#user-buttons-"+userid).fadeIn("slow");
                if((position-1) == 1){
                    userid = $(".notInCharts").attr("userid");
                    $("#user-"+userid).removeClass("notInCharts");
                    $("#user-buttons-"+userid).html("<div style='font-size:20px;'><strong>"+(position-1)+"°</strong></div>");
                    $("#user-buttons-"+userid).fadeIn("slow");
                }
                enableClick();
                hideLoading();
            });
            
        }
        });
}

function sendScore(score,videoid){
    var querystring = "action=submitScore&score="+score+"&videoid="+videoid;
    
    showLoading();
    disableClick();
    $.ajax({
        method: "get",
        url: location.href.substring(0,location.href.lastIndexOf('/')+1)+"wp-admin/admin-ajax.php",
        data: querystring, 
        success: function(response){
            alert(response)
                hideLoading();
                enableClick();
            }
            
        });
}

function disableVoting(){
    var sel = $("#ratingDuration").attr("disabled");
    if(sel != "disabled")
        $("#ratingDuration").attr("disabled","disabled");
    else
        $("#ratingDuration").removeAttr("disabled");
}

function appendButtonsContests(container,infoContainer){
    var addTitle = "<div id=\"addTitle\">"+$("#"+infoContainer).attr("vidtitle")+"</div>";
    var clr = "<div class='clr'></div>";
    var allowButton = "<div class='floatl' id='allowButton' onclick=\"allowVideoInContest('"+infoContainer+"','allow');\"></div>";
    var disallowButton = "<div class='floatl' id='disallowButton' onclick=\"allowVideoInContest('"+infoContainer+"','disallow');\"></div>";
    var playButton = "<div class='floatl' id='playButton' onclick=\"playVideoContests('"+infoContainer+"');\"></div>";
        if($("#allowButton").length == 0){
            $(container).html(addTitle+$(container).html()+disallowButton+allowButton+playButton+clr);
            $("#allowButton").fadeIn("fast");
            $("#disallowButton").fadeIn("fast");
            $("#playButton").fadeIn("fast");
            $("#addTitle").fadeIn("fast");
        }
        if($(container).find("#allowButton").length == 0 && $("#allowButton").length != 0){
            $("#allowButton").remove();
            $("#disallowButton").remove();
            $("#addTitle").remove();
            $("#playButton").remove();
            $(container).html(addTitle+$(container).html()+disallowButton+allowButton+playButton+clr);
            $("#allowButton").fadeIn("fast");
            $("#disallowButton").fadeIn("fast");
            $("#addTitle").fadeIn("fast");
            $("#playButton").fadeIn("fast");
    }
    
}

function removeButtonsContests(){
        $("#addTitle").fadeOut(100,function(){
            $(this).remove();
            });
        $("#playButton").fadeOut(100,function(){
            $(this).remove();
            });
        $("#disallowButton").fadeOut(100,function(){
            $(this).remove();
            });
        $("#allowButton").fadeOut(100,function(){
            $(this).remove();
            });
}


function playVideoContests(infoContainer){
    var newPath;
    var blackdiv = "<div class='blackdiv'></div>"
    $("#eventi").append(blackdiv);
    $(".blackdiv").css("width",$(document).width()+"px");
    $(".blackdiv").css("height",$(document).height()+"px");
    $(".blackdiv").fadeIn(700,function(){
        var plwidth = 500;
        var plheight = 350;
        var padding = 50;
        var allow = "<div onclick=\"allowVideoInContest('"+infoContainer+"','allow');\" class='allowButton'></div>";
        var disallow = "<div onclick=\"allowVideoInContest('"+infoContainer+"','disallow');\" class='disallowButton'></div>";
        newPath = $("#"+infoContainer).attr("vidpath");
        var close = "<div onclick='closeVideoPlay();' class='closeButton'></div>";
        var palette = "<div  id='palette'>"+close+allow+disallow+"</div>";
        var player = "<div id='player'><div id='player_in'></div></div>";
        var margin_lateral = parseInt(($(window).width()-plwidth)/2);
        var margin_top_bottom = parseInt(($(window).height()-plheight)/2);
        $("#eventi").append(player);
        $("#player").css("display","none");
        $("#player").css("z-index","1000");
        $("#player").css("position","absolute");
        $("#player").css("background-color","white");
        $("#player").css("padding",padding+"px");
        $("#player").css("top","0");
        $("#player").css("left","0");
        $("#player").css("margin-left",margin_lateral+"px");
        $("#player").css("margin-right",margin_lateral+"px");
        $("#player").css("margin-bottom",margin_top_bottom+"px");
        $("#player").css("margin-top",margin_top_bottom+"px");
        $("#player").css("width",plwidth+"px");
        $("#player").css("height",plheight+"px");
        $("#player").show("slow",function(){
            jwplayer("player_in").setup({
              flashplayer: '../wp-content/plugins/video-wizard/libs/jwplayer/player.swf',
              file: newPath,
              autoplay: true,
              controlbar: "bottom",
              width: plwidth,
              height: plheight,
              provider: "http",
              "http.startparam":"start"
             });
           
           $("#eventi").append(palette);
           var palette_margin_lat = $(window).width()+(2*padding)-margin_lateral+10;
           var palette_margin_top = $(window).height()-($(window).height()-(2*margin_top_bottom)+(3*padding));
           $("#palette").css('margin-left',palette_margin_lat+"px");
           $("#palette").css('margin-top',palette_margin_top+"px");
           $("#palette").show('slow');
           
        });
    });
}